﻿using KeYan.Service.Common.Enum;
using KeYan.Service.Model.UserLogin;
using KeYan.Service.IService.UserLogin;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace KeYan.Service.API.UserLogin
{
    [Route("api/[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly IUserService _Login;
        private readonly JWTTokenOptions _tokenOptions;
        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        public LoginController(JWTTokenOptions tokenOptions,IUserService login)
        {
            _tokenOptions = tokenOptions;
            _Login = login;
        }

        [HttpGet]
        [Route("Login")]
        public string Login(string uName, string uPass)
        {
            //模拟数据
            List<UserInfo> list = new List<UserInfo>();
            //{
            //    new UserInfo()  {UserId=1,UserAccount="admin",UserPassword="123456",UserName="张三		",UserExplain=null,UserStatus=0},
            //    new UserInfo()  {UserId=2,UserAccount="asd123",UserPassword="112233",UserName="李五一	",UserExplain=null,UserStatus=0},
            //    new UserInfo()  {UserId=3,UserAccount="ahusan",UserPassword="152369",UserName="金萱虎	",UserExplain=null,UserStatus=0}
            //};
            UserInfo user = new UserInfo();
            //查询
            user = list.Where(p => p.UserName == uName && p.PassWord == uPass).FirstOrDefault();
            //返回的结果
            bool result = false;
            //返回的token的结果集默认为null
            TokenResult JwtToken = new TokenResult();
            if (user != null)
            {
                //生成teken
                JwtToken = new JwtTokenHelper().AuthorizeToken(uName, _tokenOptions);
                return JsonConvert.SerializeObject(new
                {
                    result = true,
                    JwtToken
                });
            }
            //登录失败，返回默认值 
            return JsonConvert.SerializeObject(new
            {
                result,
                JwtToken
            });
        }
        [HttpGet,Route("GetIdsTokenAsync")]
        public Task<Resilt<string>> GetIdsTokenAsync(string LoginName, string LoginPwd)
        {
            return _Login.GetIdsTokenAsync(LoginName, LoginPwd);
        }
        public Task<Resilt<int>> PhoneLogin(string Tel,string Pwd)
        {
            return _Login.PhoneLogin(Tel, Pwd);
        }
    }

}
